Const TypeId
and non-'static TypeId
Const TypeId
This crate provides ConstTypeId
, which is like core::any::TypeId
but is
constructible in const in stable Rust. (The standard library's TypeId's is
nightly-only to construct in const; the tracking issue for this is
rust#77125.)
Being able to construct ConstTypeId
in const makes it suitable for use cases
that rely on static promotion:
use ;
use ptr;
use ConstTypeId;
and in associated constants:
use ConstTypeId;
Non-'static TypeId
This crate provides typeid::of
, which takes an arbitrary non-'static type
T
and produces the TypeId
for the type obtained by replacing all lifetimes
in T
by 'static
, other than higher-rank lifetimes found in trait objects.
For example if T
is &'b dyn for<'a> Trait<'a, 'c>
, then typeid::of::<T>()
produces the TypeId of &'static dyn for<'a> Trait<'a, 'static>
.
It should be obvious that unlike with the standard library's TypeId,
typeid::of::<A>() == typeid::of::<B>()
does not mean that A
and B
are
the same type. However, there is a common special case where this behavior is
exactly what is needed. If:
A
is an arbitrary non-'static type parameter, andB
is 'static, and- all types with the same id as
B
are also 'static
then typeid::of::<A>() == typeid::of::<B>()
guarantees that A
and B
are
the same type.
use TypeId;
use slice;